package com.xltt.vehicleControl.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Md5Util {

    /**
     * 是否已经加密过的密码，因为手机prd密码长度为8-12位，只有md5密码才默认32位
     * @param str
     * @return
     */
    public static boolean isMD5Password(String str){
        return str.length() > 31;
    }

    /**
     * 对密码进行加密
     *
     * @param pwd 输入用户密码
     * @return 返回加密后的用户密码
     */
    public static String encrypt(String pwd) {
        MessageDigest messageDigest = null;
        StringBuffer md5StrBuff = new StringBuffer();
        try {
            messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.reset();
            messageDigest.update(pwd.getBytes());
            byte[] byteArray = messageDigest.digest();
            for (int i = 0; i < byteArray.length; i++) {
                if (Integer.toHexString(0xFF & byteArray[i]).length() == 1) {
                    md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
                } else {
                    md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
                }
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return md5StrBuff.toString();
    }


}
